#include "qplotsettings.h"

QPlotSettings::QPlotSettings(QWidget *parent) :
  QWidget(parent)
{
  init();
  //connect(snapButton,SIGNAL(clicked()),this,SIGNAL(takeSnapshot()));
}

void QPlotSettings::newVariable(variable newvar)
{
  int rowNumber = variableListWidget->rowCount();
  variableListWidget->insertRow(rowNumber);
  //rowNumber++;
  QTableWidgetItem *newItem = new QTableWidgetItem(newvar.name);
  variableListWidget->setItem(rowNumber,0,newItem);

  QTableWidgetItem *checkBoxItem = new QTableWidgetItem();
  checkBoxItem->setCheckState(Qt::Unchecked);
  variableListWidget->setItem(rowNumber, 1, checkBoxItem);

  if(newvar.type==BYTEARRAY)
  {
    Qt::ItemFlags flags = variableListWidget->item(rowNumber,1)->flags();
    flags &= ~Qt::ItemIsEnabled;
    flags &= ~Qt::Checked;
    variableListWidget->item(rowNumber,1)->setFlags(flags);
  }
  variableList.append(newvar);

}

void QPlotSettings::editVariable(int index,variable newvar)
{
  if(variableList.at(index).type==BYTEARRAY)
  {
    if(newvar.type == NUMBER)
    {
      Qt::ItemFlags flags = variableListWidget->item(index,1)->flags();
      flags |= Qt::ItemIsEnabled;
      variableListWidget->item(index,1)->setFlags(flags);
    }
  }
  else
  {
    if(newvar.type == BYTEARRAY)
    {
      Qt::ItemFlags flags = variableListWidget->item(index,1)->flags();
      flags &= ~Qt::ItemIsEnabled;
      flags &= ~Qt::Checked;
      variableListWidget->item(index,1)->setFlags(flags);
    }
  }
  QTableWidgetItem *newItem = new QTableWidgetItem(newvar.name);
  variableListWidget->setItem(index,0,newItem);
  variableList.removeAt(index);
  variableList.insert(index,newvar);

  // Make table with checkbox values:
  QList<bool> checkList;
  for(int i=0;i<variableListWidget->rowCount();i++)
  {
    if(variableListWidget->item(i,1)->checkState()==Qt::Checked)
      checkList.append(true);
    else
      checkList.append(false);
  }
  emit plotSelectionChanged(checkList);
}

void QPlotSettings::removeVariable(int index)
{
  variableListWidget->removeRow(index);
  variableList.removeAt(index);

  // Make table with checkbox values:
  QList<bool> checkList;
  for(int i=0;i<variableListWidget->rowCount();i++)
  {
    if(variableListWidget->item(i,1)->checkState()==Qt::Checked)
      checkList.append(true);
    else
      checkList.append(false);
  }
  emit plotSelectionChanged(checkList);
}

void QPlotSettings::boxClicked()
{
  // Make table with checkbox values:
  QList<bool> checkList;
  for(int i=0;i<variableListWidget->rowCount();i++)
  {
    if(variableListWidget->item(i,1)->checkState()==Qt::Checked)
      checkList.append(true);
    else
      checkList.append(false);
  }
  emit plotSelectionChanged(checkList);
}

void QPlotSettings::buildTable(QList<variable> newList)
{

  variableListWidget->clear();
  variableListWidget->setRowCount(0);
  variableListWidget->setColumnCount(2);
  QStringList columnNames;
  columnNames.append("Variable");
  columnNames.append("Plot");
  variableListWidget->horizontalHeader()->setResizeMode(0,QHeaderView::Fixed);
  variableListWidget->horizontalHeader()->setResizeMode(1,QHeaderView::Fixed);
  variableListWidget->setHorizontalHeaderLabels(columnNames);
  for(int i=0;i<newList.size();i++)
  {
    variableListWidget->insertRow(i);
    QTableWidgetItem *newItem = new QTableWidgetItem(newList.at(i).name);
    variableListWidget->setItem(i,0,newItem);
    QWidget* wdg = new QWidget;
    QCheckBox *newCheck = new QCheckBox;
    QHBoxLayout* layout = new QHBoxLayout(wdg);
    layout->addWidget(newCheck);
    layout->setAlignment(Qt::AlignHCenter);
    wdg->setLayout(layout);
    layout->setMargin(0);
    variableListWidget->setCellWidget(i, 1, wdg);

    //    QTableWidgetItem *newBox = new QTableWidgetItem();
    //    newBox->setCheckState(Qt::Unchecked);
    //   newBox->setTextAlignment(Qt::AlignHCenter);
    //variableListWidget->setItem(i,1,newBox);


    //   variableListWidget->setCellWidget(i,1,newCheck);
  }
}

void QPlotSettings::setcanvasColor()
{
  QColor color;
  color = QColorDialog::getColor(QColor(bgcolorLabel->palette().background().color()));
  if (color.isValid()) {
    bgcolorLabel->setPalette(QPalette(color));
    bgcolorLabel->setAutoFillBackground(true);
    emit canvasColor(color);
  }
}

void QPlotSettings::setgridColor()
{
  QColor color;
  color = QColorDialog::getColor(QColor(gridcolorLabel->palette().background().color()));
  if (color.isValid()) {
    gridcolorLabel->setPalette(QPalette(color));
    gridcolorLabel->setAutoFillBackground(true);
    emit gridColor(color);
  }
}

void QPlotSettings::setcurveColor()
{
  QColor color;
  color = QColorDialog::getColor(QColor(ptscolorLabel->palette().background().color()));
  if (color.isValid()) {
    ptscolorLabel->setPalette(QPalette(color));
    ptscolorLabel->setAutoFillBackground(true);
    emit curveColor(color);
  }
}

void QPlotSettings::init()
{
  // GUI COMPONENTS
  // Points
  int colorStyle = QFrame::Sunken | QFrame::Panel;

  dataBox = new QGroupBox("Data");
  dataBox->setAlignment(Qt::AlignHCenter);
  dataBox->setMaximumSize(300,125);
  ptsDisplayed = new QLabel("Points");
  noptsSpin = new QSpinBox;
  noptsSpin->setMinimum(10);
  noptsSpin->setMaximum(2000);
  noptsSpin->setValue(50);
  noptsSpin->setAlignment(Qt::AlignHCenter);
  noptsSpin->setFixedSize(60,23);
  ptscolorButton = new QPushButton("Curve color");
  ptscolorButton->setFixedSize(80,23);
  ptscolorLabel = new QLabel;
  ptscolorLabel->setFixedSize(40,20);
  ptscolorLabel->setFrameStyle(colorStyle);
  ptscolorLabel->setPalette(QPalette(Qt::white));
  ptscolorLabel->setAutoFillBackground(true);

  ptswidthLabel = new QLabel("Curve width");
  ptswidthSpin = new QSpinBox;
  ptswidthSpin->setMinimum(1);
  ptswidthSpin->setMaximum(10);
  ptswidthSpin->setValue(3);
  ptswidthSpin->setAlignment(Qt::AlignHCenter);
  ptswidthSpin->setFixedSize(60,23);

  //snapButton = new QPushButton("Take Snapshot");

  // Canvas
  canvasBox = new QGroupBox("Canvas");
  canvasBox->setAlignment(Qt::AlignHCenter);
  canvasBox->setMaximumSize(300,125);
  bgcolorButton = new QPushButton("Canvas color");
  bgcolorButton->setFixedSize(80,23);
  bgcolorLabel = new QLabel;
  bgcolorLabel->setFixedSize(40,20);
  bgcolorLabel->setAutoFillBackground(true);
  bgcolorLabel->setFrameStyle(colorStyle);
  bgcolorLabel->setPalette(QPalette(Qt::black));

  gridCheck = new QCheckBox;
  gridCheckLabel = new QLabel("Show grid");
  gridcolorButton = new QPushButton("Grid color");
  gridcolorButton->setFixedSize(80,23);
  gridcolorLabel = new QLabel;
  gridcolorLabel->setFixedSize(40,20);
  gridcolorLabel->setAutoFillBackground(true);
  gridcolorLabel->setFrameStyle(colorStyle);
  gridcolorLabel->setPalette(QPalette(Qt::white));

  // Variable list
  variableListWidget = new QTableWidget;
  variableListWidget->setMaximumWidth(300);
  variableListWidget->setColumnCount(2);
  QStringList columnNames;
  columnNames.append("Variable");
  columnNames.append("Plot");
  variableListWidget->horizontalHeader()->setResizeMode(0,QHeaderView::Fixed);
  variableListWidget->horizontalHeader()->setResizeMode(1,QHeaderView::Fixed);
  variableListWidget->setHorizontalHeaderLabels(columnNames);
  variableListWidget->setColumnWidth(0,80);
  variableListWidget->setColumnWidth(1,30);

  // Layout
  pointsLayout = new QGridLayout;

  QHBoxLayout *pointsLabelLayout = new QHBoxLayout;
  pointsLabelLayout->addSpacing(20);
  pointsLabelLayout->addWidget(ptsDisplayed);
  pointsLabelLayout->addSpacing(20);
  pointsLayout->addLayout(pointsLabelLayout,0,0);
  QHBoxLayout *pointsSpinLayout = new QHBoxLayout;
  pointsSpinLayout->addSpacing(20);
  pointsSpinLayout->addWidget(noptsSpin);
  pointsSpinLayout->addSpacing(20);
  pointsLayout->addLayout(pointsSpinLayout,0,1);
  QHBoxLayout *widthLabelLayout = new QHBoxLayout;
  widthLabelLayout->addSpacing(5);
  widthLabelLayout->addWidget(ptswidthLabel);
  widthLabelLayout->addSpacing(5);
  pointsLayout->addLayout(widthLabelLayout,1,0);
  QHBoxLayout *widthSpinLayout = new QHBoxLayout;
  widthSpinLayout->addSpacing(5);
  widthSpinLayout->addWidget(ptswidthSpin);
  widthSpinLayout->addSpacing(5);
  pointsLayout->addLayout(widthSpinLayout,1,1);
  QHBoxLayout *dataColorLabelLayout = new QHBoxLayout;
  dataColorLabelLayout->addSpacing(10);
  dataColorLabelLayout->addWidget(ptscolorLabel);
  dataColorLabelLayout->addSpacing(10);
  pointsLayout->addLayout(dataColorLabelLayout,2,0);
  QHBoxLayout *dataColorButtonLayout = new QHBoxLayout;
  dataColorButtonLayout->addSpacing(10);
  dataColorButtonLayout->addWidget(ptscolorButton);
  dataColorButtonLayout->addSpacing(10);
  pointsLayout->addLayout(dataColorButtonLayout,2,1);

  dataBox->setLayout(pointsLayout);

  canvasLayout = new QGridLayout;
  QHBoxLayout *bgLabelLayout = new QHBoxLayout;
  bgLabelLayout->addSpacing(5);
  bgLabelLayout->addWidget(bgcolorLabel);
  bgLabelLayout->addSpacing(5);
  canvasLayout->addLayout(bgLabelLayout,0,0);
  QHBoxLayout *bgColorLayout = new QHBoxLayout;
  bgColorLayout->addSpacing(10);
  bgColorLayout->addWidget(bgcolorButton);
  bgColorLayout->addSpacing(10);
  canvasLayout->addLayout(bgColorLayout,0,1);
  QHBoxLayout *gridLabelLayout = new QHBoxLayout;
  gridLabelLayout->addSpacing(10);
  gridLabelLayout->addWidget(gridcolorLabel);
  gridLabelLayout->addSpacing(10);
  canvasLayout->addLayout(gridLabelLayout,1,0);
  QHBoxLayout *gridColorLayout = new QHBoxLayout;
  gridColorLayout->addSpacing(10);
  gridColorLayout->addWidget(gridcolorButton);
  gridColorLayout->addSpacing(10);
  canvasLayout->addLayout(gridColorLayout,1,1);
  QHBoxLayout *gridCheckLabelLayout = new QHBoxLayout;
  gridCheckLabelLayout->addSpacing(5);
  gridCheckLabelLayout->addWidget(gridCheckLabel);
  gridCheckLabelLayout->addSpacing(5);
  canvasLayout->addLayout(gridCheckLabelLayout,2,0);
  QHBoxLayout *gridCheckLayout = new QHBoxLayout;
  gridCheckLayout->addSpacing(50);
  gridCheckLayout->addWidget(gridCheck);
  gridCheckLayout->addSpacing(50);
  canvasLayout->addLayout(gridCheckLayout,2,1);

  canvasBox->setLayout(canvasLayout);

  settingsLayout = new QVBoxLayout;
  settingsLayout->addWidget(dataBox);
  settingsLayout->addWidget(canvasBox);

  mainLayout = new QHBoxLayout;
  mainLayout->addLayout(settingsLayout);
  mainLayout->addWidget(variableListWidget);

  this->setLayout(mainLayout);
  connect(noptsSpin,SIGNAL(valueChanged(int)),this,SIGNAL(buffSize(int)));
  connect(bgcolorButton,SIGNAL(clicked()),this,SLOT(setcanvasColor()));
  connect(gridCheck,SIGNAL(toggled(bool)),this,SIGNAL(gridToggle(bool)));
  connect(gridcolorButton,SIGNAL(clicked()),this,SLOT(setgridColor()));
  connect(ptscolorButton,SIGNAL(clicked()),this,SLOT(setcurveColor()));
  connect(ptswidthSpin,SIGNAL(valueChanged(int)),this,SIGNAL(curveWidth(int)));
  connect(variableListWidget,SIGNAL(itemClicked(QTableWidgetItem*)),this,SLOT(boxClicked()));
}
